<?php

//

namespace app\console\controller;

use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\Brand as BrandModel;
use app\console\model\BrandType as BrandTypeModel;

class Brand extends Controller
{
    public function alipay(){
        $this->curl_request('http://app104.app.longcai.net/index.php/interfaces/index/index');

    }
  public function curl_request($url,$post='',$cookie='', $returnCookie=0){
             $curl = curl_init();
          curl_setopt($curl, CURLOPT_URL, $url);
          curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
          curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
          curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
          if($post) {
                     curl_setopt($curl, CURLOPT_POST, 1);
             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
         }
         if($cookie) {
                     curl_setopt($curl, CURLOPT_COOKIE, $cookie);
         }
         curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
         curl_setopt($curl, CURLOPT_TIMEOUT, 10);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         $data = curl_exec($curl);
         if (curl_errno($curl)) {
                     return curl_error($curl);
         }
         curl_close($curl);
         if($returnCookie){
                     list($header, $body) = explode("\r\n\r\n", $data, 2);
             preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
             $info['cookie']  = substr($matches[1][0], 1);
             $info['content'] = $body;
             return $info;
         }else{
                     return $data;
         }
 }

    /**
     * 品牌列表
     * @param Request $request
     * @param BrandModel $brand
     * @return mixed
     */
    public function index(Request $request, BrandModel $brand, BrandTypeModel $brandType)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        if (!empty($inquire['title'])) $condition['name'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type_id'])) $condition['type_id'] = ['eq', $inquire['type_id']];

        // 读取参数
        $data = $brand->where($condition)->order(['sort' => 'desc', 'id' => 'desc'])->paginate(10, false, ['query' => $inquire]);

        $classify = $brandType::all();

        return $this->fetch('index', [
            'mate_title' => '品牌列表',
            'data'       => $data,
            'classify'   => $classify
        ]);
    }


    /**
     * 新增品牌
     * @param Request $request
     * @param BrandModel $brand
     * @param BrandTypeModel $brandType
     * @return mixed
     */
    public function create(Request $request, BrandModel $brand, BrandTypeModel $brandType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Brand');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 品牌分类写死
            $data['type_id'] = 1;

            // 提交数据
            $dispose = $brand->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('Brand/index'));
            }

            fail('操作失败');

        }

        $list = $brandType::all();


        return $this->fetch('create', [
            "mate_title" => '品牌添加',
            "data"       => $list
        ]);

    }


    /**
     * 品牌删除
     * @param $id
     * @param BrandModel $brand
     */
    public function destroy($id, BrandModel $brand)
    {

        if ($brand::destroy($id)) {
            win('操作成功', Url::build('Brand/index'));
        }

        fail('操作失败');

    }

    // /*
    //  * 全选删除
    //  */
    // public function destroyAll($id)
    // {
    //
    //     if (BrandModel::destroy($id)) {
    //         win('操作成功', Url::build('Brand/index'));
    //     }
    //
    //     fail('操作失败');
    //
    // }


    /**
     * 编辑品牌
     * @param $id
     * @param Request $request
     * @param BrandModel $brand
     * @param BrandTypeModel $brandType
     * @return mixed
     */
    public function update($id, Request $request, BrandModel $brand, BrandTypeModel $brandType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('Brand');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $brand->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('Brand/index'));
            }

            fail('操作失败');

        }

        $item = $brand::get($id);
        $list = $brandType::all();


        return $this->fetch('create', [
            "mate_title" => '编辑品牌',
            "item"       => $item,
            "data"       => $list

        ]);
    }


    /**
     * 更改状态
     * @param Request $request
     * @param BrandModel $brand
     * @return bool|int
     */
    public function auditing(Request $request, BrandModel $brand)
    {
        $id = $request->instance()->param('id');

        $status = $brand->where('id', $id)->value('recommend') == '1' ? 2 : 1;

        if ($brand->where('id', $id)->update(['recommend' => $status])) {

            return $status;
        }

        return false;
    }


    /**
     * 更改排序
     * @param Request $request
     * @param BrandModel $brand
     */
    public function sorting(Request $request, BrandModel $brand)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        foreach ($id as $key => $value) {

            $brand->where('id', $value)->update(['sort' => $sort[$key]]);

        }

        win('操作成功', Url::build('Brand/index'));
    }
}
